package com.edu.glutdataconvertserver.config.mysql;

import com.google.common.collect.Maps;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

/**
 * mybatis拦截器配置
 *
 * @Author: dwzhao
 * @Date: 2019/8/20
 */
@Configuration
public class MybatisInterceptorConfig {


    @Autowired(required = false)
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @PostConstruct
    public void addMysqlInterceptor() {
        TableSplitInterceptor tableSplitInterceptor = new TableSplitInterceptor();

        StrategyManager strategyManager = new StrategyManager();
        Map<String, String> strategyMap = Maps.newHashMap();
        strategyMap.put(MysqlConstants.SHARDING_STRATEGYNAME, MysqlConstants.SHARDING_STRATEGY);
        strategyManager.setStrategies(strategyMap);
        tableSplitInterceptor.setStrategyManager(strategyManager);

        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(tableSplitInterceptor);
        }
    }
}
